package acm.蓝桥杯二分前缀;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

// todo 二分前缀和，记得补题
public class 天上的星星 {
    static int [][] star;
    public static void main(String[] args) {
        Reader sc=new Reader();
        int n = sc.nextInt();
        star = new int[n][3];
        for (int i = 0; i < n; i++) {
            star[i][0] = sc.nextInt();
            star[i][1] = sc.nextInt();
            star[i][2] = sc.nextInt();
        }
        Arrays.sort(star,(a,b)->a[0]-b[0]);
        int m = sc.nextInt();
        for (int i = 0; i < m; i++) {
            int[] lb={sc.nextInt(), sc.nextInt()};
            int[] rt={sc.nextInt(), sc.nextInt()};
            int res = 0;
            int l = ceilSearch(0,star.length-1,lb[0]);
            int r = floorSearch(l,star.length-1,rt[0]);
            for (int j = l; j <= r; j++) {
                if (star[j][1]>=lb[1]&&star[j][1]<=rt[1]){
                    res+=star[j][2];
                }
            }
            System.out.println(res);
        }
    }
    //lr上找第一个大于等于x的数
    public static int ceilSearch(int l,int r, int x){
        while(l<=r){
            int mid = l+((r-l)>>1);
            if(x<=star[mid][0]){
                r = mid-1;
            }else{
                l = mid+1;
            }
        }
        if (l>=star.length){
            return star.length-1;
        }else {
            return l;
        }
    }

    public static int floorSearch(int l,int r, int x){
        while(l<=r){
            int mid = l+((r-l)>>1);
            if(x<star[mid][0]){
                r = mid-1;
            }else{
                l = mid+1;
            }
        }
        if (r<0){
            return 0;
        }else {
            return r;
        }
    }

    static class Reader {
        private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        private StringTokenizer tokenizer = new StringTokenizer("");

        private String innerNextLine() {
            try {
                return reader.readLine();
            } catch (IOException ex) {
                return null;
            }
        }

        public boolean hasNext() {
            while (!tokenizer.hasMoreTokens()) {    //内存中没有
                String nextLine = innerNextLine();
                if (nextLine == null) {     //且没有下一行
                    return false;
                }
                tokenizer = new StringTokenizer(nextLine);  //读入一行
            }
            return true;
        }

        public String nextLine() {
            tokenizer = new StringTokenizer("");    //清空内存中的字符
            return innerNextLine();
        }
        public void clear(){
            tokenizer = new StringTokenizer("");
        }

        public String next() {
            hasNext();
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble(){
            return Double.parseDouble(next());
        }
//    public BigInteger nextBigInt() {
//        return new BigInteger(next());
//    }
    }

}
